package com.rakuten.api.mule.transformers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.derby.iapi.services.daemon.DaemonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.mule.api.MuleMessage;
import org.mule.api.annotations.Transformer;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;
import org.mule.transformer.types.DataTypeFactory;
import org.mule.transport.http.HttpConstants;
import org.mule.util.FileUtils;

import com.rakuten.api.mule.CommonFlowConstants;
import com.rakuten.api.mule.model.DisplayStartDate;
//import com.rakuten.api.mule.exceptions.InboundPropertyNotFoundException;
import com.rakuten.api.mule.model.Price;
import com.rakuten.api.mule.model.Product;
import com.rakuten.api.mule.model.RequestData;
import com.rakuten.api.mule.model.Variants;
import com.rakuten.api.mule.model.shop.Shop;

/**
 * Validate input parameters and set MuleMessage.
 * 
 * @author saito_shinya@ogis-ri.co.jp
 */
public class InitializationParameters extends AbstractMessageTransformer {

	public InitializationParameters() {
		super();
		this.registerSourceType(DataTypeFactory.create(Product.class));
		setReturnDataType(DataTypeFactory.create(Map.class));
	}

	@Override
	public Object transformMessage(MuleMessage message, String outputEncoding)
			throws TransformerException {
		// Set original payload to session variable

		// message.setProperty(CommonFlowConstants.CURRENT_FLOW_OBJECT, this
		// .getClass().getSimpleName(), PropertyScope.SESSION);
		try {
			String url = "";
			// System.out.println("Payload"+message.getPayloadAsString());
			Product product = message.getPayload(Product.class);
			// get mearchantId
			String merchantId = message.getInboundProperty("merchantId");
			message.setProperty("merchantId",merchantId, PropertyScope.SESSION);

			
			message.setProperty("productInfo", product, PropertyScope.SESSION);
			List<DisplayStartDate> displayStartDateList = product
					.getDisplayStartDates();
			// //Map for saving Shop URL
			Map<String, String> shopUrlMap = new HashMap<String, String>();
			// /****gather all shopurl into one map add by jo***/
			for (int i = 0; i < displayStartDateList.size(); i++) {
				// if there is duplicated shopurl, overwirte it
				shopUrlMap.put(displayStartDateList.get(i).getShopURL(),
						displayStartDateList.get(i).getMarketplaceIdentifier());
			}
			// Save shop URL to payload
			message.setPayload(shopUrlMap);

			return message;
		} catch (Exception ex) {
			throw new TransformerException(this, ex);
		}
	}
	/*
	 * private void checkInboundProperty(MuleMessage message, String
	 * propertyName) { String propertyValue =
	 * message.getInboundProperty(propertyName); if (propertyValue != null) {
	 * message.setProperty(propertyName, propertyValue, PropertyScope.SESSION);
	 * } else { throw new InboundPropertyNotFoundException(propertyName); } }
	 */
	/**
	 * TODO Temporary implementation. implements MongoDB version.
	 */
	/*
	 * private Map<String, Object> getApiFlowInfoFromFile(String apiKey) throws
	 * Exception { ObjectMapper mapper = new ObjectMapper(); Map<String, Object>
	 * routingData = mapper.readValue(
	 * FileUtils.newFile("data/routing-data_dev.json"), Map.class);
	 * 
	 * return routingData; }
	 * 
	 * private Map<String, Object> getApiFlowInfoFromMongo(String apiKey) throws
	 * Exception {
	 * 
	 * MongoClient mongoClient = new MongoClient(mongodbHost,
	 * Integer.valueOf(mongodbPort)); DB db =
	 * mongoClient.getDB("routing_table");
	 * 
	 * DBCollection coll = db.getCollection("apis"); BasicDBObject query = new
	 * BasicDBObject("apiName", apiKey); DBObject dbObject =
	 * coll.findOne(query);
	 * 
	 * return (Map<String, Object>) dbObject; }
	 */
}
